package com.ifdom.thread.multthread;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import javax.annotation.security.RunAs;
import java.util.concurrent.*;
import java.util.concurrent.ThreadFactory;

/**
 * 创建线程池的正确方法 https://cloud.tencent.com/developer/article/1638175
 **/
public class ThreadPoolCreate {
    private static  ThreadFactory NAMED_THREAD_FACTORY = new ThreadFactoryBuilder().setNameFormat("demo-pool-%d").build();

    private static  ExecutorService POOL = new ThreadPoolExecutor(
            5, 100, 0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<>(1024), NAMED_THREAD_FACTORY
    );

    public static void main(String[] args) {
        for (int i = 0; i < 20; i++) {
            POOL.execute(new SubThread());
        }

    }

    private static class SubThread implements Runnable {

        @Override
        public void run() {
            System.out.println("====" + Thread.currentThread().getState());
        }
    }
}
